from rest_framework.viewsets import ModelViewSet

from apps.admin.utils import PageNum
from apps.goods.models import SKU
from apps.admin.serializers.sku import SKUModelSerializer


class SKUModelViewSet(ModelViewSet):
    # queryset = SKU.objects.all()

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword:
            return SKU.objects.filter(name__contains=keyword)

        return SKU.objects.all()

    pagination_class = PageNum
    serializer_class = SKUModelSerializer


####    三级展示    ######
from apps.goods.models import GoodsCategory
from rest_framework.generics import ListAPIView
from apps.admin.serializers.sku import GoodsCategoryModelSerializer

class GoodsCategoryAPIView(ListAPIView):
    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = GoodsCategoryModelSerializer


######    SPU表   ############
from apps.goods.models import SPU
from apps.admin.serializers.sku import GoodsSimpleSerializer

class GoodsSimpleView(ListAPIView):
    serializer_class = GoodsSimpleSerializer
    queryset = SPU.objects.all()


#####    SPU规格和选项    ######
from rest_framework.views import APIView
from apps.goods.models import SPUSpecification,SpecificationOption
from apps.admin.serializers.sku import SpecsModelSerializer
from rest_framework.response import Response
class SPUSpecAPIView(APIView):
    def get(self,request,spu_id):
        specs = SPUSpecification.objects.filter(spu_id=spu_id)
        serializer = SpecsModelSerializer(specs,many=True)
        return Response(serializer.data)